import java.util.Scanner;
public class MaximumSubarray {
    public static int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int maxSoFar = nums[0],maxEndingHere = nums[0];
        for (int i = 1; i < nums.length; i++) {
            maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
            maxSoFar = Math.max(maxSoFar, maxEndingHere);
        }
        return maxSoFar;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("=== 最大子数组和计算器 ===");
        System.out.print("请输入数组的长度: ");
        int n = scanner.nextInt();
        int[] nums = new int[n];
        System.out.println("请输入 " + n + " 个整数:");
        for (int i = 0; i < n; i++) {
            System.out.print("元素 " + (i + 1) + ": ");
            nums[i] = scanner.nextInt();
        }
        System.out.print("你输入的数组是: [");
        for (int i = 0; i < n; i++) {
            System.out.print(nums[i]);
            if (i < n - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("]");
        int result = maxSubArray(nums);
        System.out.println("最大连续子数组的和为: " + result);
        scanner.close();
    }
}